package it.massetti.scoponescientifico.game;

import java.util.ArrayList;
import java.util.List;

public class PlayCardAction implements GameAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5041850824868932209L;
	private Card card;
	private List<Card> cardsToTake;
	private Integer caller;
	private List<NotificationMessage> listOfNotifications;
	
	public PlayCardAction (Card card, List<Card> cardsToTake, Integer caller) {
		this.card = card;
		this.cardsToTake = cardsToTake;
		this.caller = caller;
	}

	@Override
	public Boolean isLegal(Game state) {
		if(caller==state.getCurrentPlayer().getNumber()) {
			if(card.number.getValue()==state.getDetck().sumCards(cardsToTake)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<NotificationMessage> apply(Game state) {
		listOfNotifications = new ArrayList<NotificationMessage>();
		try {
			state.playCard(state.getCurrentPlayer(), card, cardsToTake);
		} catch (noCardException e) {
			listOfNotifications.add(new MoveNotValidNotification());
		} catch (AceScopeException e) {
			listOfNotifications.add(new TakeAllCardsNotification());
		} catch (ScopeException e) {
			listOfNotifications.add(new TakeAllCardsNotification());
		} catch (HandEndException e) {
			listOfNotifications.add(new EndHandNotification());
		} catch (TakeCardsException e) {
			listOfNotifications.add(new TakeCardsNotification());
		}
		return listOfNotifications;
	}

}
